import torch

# torch实现的是张量运算，张量是多维数组
def test_torch():
    tensor = torch.tensor([[1,2],[3,4]],dtype=torch.float32)
    x=torch.tensor([1,2,3,4],dtype=torch.float32)
    y= torch.randn(3,4,dtype=torch.float32)
    z= x+y

    print(f"x：{x}")
    print(f"y：{y}")
    print(f"z：{z}")
    print(f"z均值：{z.mean()}")

    torch.save(tensor,"./study/torch.txt")

    data = torch.load("./study/torch.txt",weights_only=True)
    print(f"torch.txt：{data}")

    print(f"维度: {tensor.shape}")
    print(f"均值：{tensor.mean()}")
    print(f"最大值：{tensor.max()}")
    print(f"最小值：{tensor.min()}")
    print(f"元素个数：{tensor.numel()}")
    print(f"转置：{tensor.t()}")
    print(f"矩阵乘法：{tensor @ tensor.T}")
    print(f"运算：{tensor+1}")
    print(f"绝对值：{torch.abs(tensor)}")
    print(f"指数：{torch.exp(tensor)}")
    print(f"对数：{torch.log(tensor)}")
    print(f"幂：{torch.pow(tensor, 2)}")
    print(f"开方：{torch.sqrt(tensor)}")
    print(f"正弦：{torch.sin(tensor)}")
    print(f"余弦：{torch.cos(tensor)}")

if __name__ == "__main__":
    print("hello test torch")
    test_torch()
